package com.wthink.hzgj.msgList.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.wthink.card.models.GMBInfoBundle
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.msgList.`interface`.MsgCallback
import com.wthink.hzgj.util.PreferencesUtils
import com.wthink.hzgj.util.toast

class MsgListViewmodel :ViewModel(){

    //val msgDetail = MutableLiveData(NoticeMsg("","",0))
    //val msgDetail = MutableLiveData("")
    val msgDetail00 = MutableLiveData(NoticeMsg("","","",false,false))

    val stringToArrryList = PreferencesUtils.stringToList()
    //var stringToList  = arrayListOf<String>("哈哈哈0","哈哈哈1","哈哈哈2","哈哈哈3","哈哈哈4","哈哈哈5","哈哈哈6","哈哈哈7")


    lateinit var msgCallback: MsgCallback

    fun setMsg1Callback(callBack: MsgCallback) {
        msgCallback = callBack
    }

    fun start() {
        if (stringToArrryList.size > 0) {
            msgDetail00.postValue(stringToArrryList[0])
     //       msgCallback.msgCallback(stringToArrryList[0])
            GMBInfoBundle.setMsgInfoIndex(0)
        } else {
       //     msgCallback.msgCallback(NoticeMsg("无更多历史消息" ,"","-99",true))
            msgDetail00.postValue(NoticeMsg("无更多历史消息" ,"","-99",true,false))
        }
    }

    fun previous() {
        val stringToList = PreferencesUtils.stringToList()
        val msgInfoIndex = GMBInfoBundle.getMsgInfoIndex()
        Log.d("indexxxx","previous"+msgInfoIndex + "->" + stringToList.size)
        if (msgInfoIndex<=0) {
            toast(BusApplication.busContext, "无更多历史消息")
        } else {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex-1)
            android.util.Log.d("msglist","previous-->"+(msgInfoIndex-1))
            //msgDetail.postValue(stringToList[msgInfoIndex-1])
            msgDetail00.postValue(stringToList[msgInfoIndex-1])
            //  msgCallback.msgCallback(stringToList[msgInfoIndex-1])
        }
    }

    fun next() {
      /*  val msgInfoIndex = GMBInfoBundle.getMsgInfoIndex()
        if (msgInfoIndex >= stringToList.size-1) {
            toast(BusApplication.busContext, "无更多历史消息")
        } else {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex+1)
            android.util.Log.d("msglist","next-->"+(msgInfoIndex+1))
            msgDetail.postValue(stringToList[msgInfoIndex+1])
        } */

        val stringToList = PreferencesUtils.stringToList()
        val msgInfoIndex = GMBInfoBundle.getMsgInfoIndex()
        Log.d("indexxxx","next"+msgInfoIndex + "->" + stringToList.size)

        if (msgInfoIndex >= stringToList.size-1) {
            toast(BusApplication.busContext, "无更多历史消息")
        } else {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex+1)
            android.util.Log.d("msglist","next-->"+(msgInfoIndex+1))
            msgDetail00.postValue(stringToList[msgInfoIndex+1])
         //   msgCallback.msgCallback(stringToList[msgInfoIndex+1])
        }

        if (msgInfoIndex >= PreferencesUtils.stringToList().size - 1) {

        }
    }

    fun deleteOne() {
      /*  val msgInfoIndex = GMBInfoBundle.getMsgInfoIndex()
        stringToList.removeAt(msgInfoIndex)

        if (msgInfoIndex == 0) {
            GMBInfoBundle.setMsgInfoIndex(0)
        } else if (msgInfoIndex == stringToList.size) {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex-1)
        } else {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex)
        }

        android.util.Log.d("msglist","delete-->"+GMBInfoBundle.getMsgInfoIndex())

        if (msgInfoIndex < stringToList.size) {
            msgDetail.postValue(stringToList[msgInfoIndex])
        } else {
            msgDetail.postValue(stringToList[stringToList.size-1])
        }*/

        if (stringToArrryList.size == 0) {
            toast(BusApplication.busContext, "无更多历史消息")
            return
        }
        val msgInfoIndex = GMBInfoBundle.getMsgInfoIndex()
        stringToArrryList.removeAt(msgInfoIndex)
        var stringToArrryLists : MutableList<NoticeMsg> = mutableListOf()

        for (noticeMsg in stringToArrryList) {
            stringToArrryLists.add(NoticeMsg(noticeMsg.msg,noticeMsg.time,""+(stringToArrryList.indexOf(noticeMsg)+1),noticeMsg.isLastMsg,false))
        }

        if (msgInfoIndex == 0) {
            GMBInfoBundle.setMsgInfoIndex(0)
        } else if (msgInfoIndex == stringToArrryLists.size) {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex-1)
        } else {
            GMBInfoBundle.setMsgInfoIndex(msgInfoIndex)
        }

        android.util.Log.d("msglist","delete-->"+GMBInfoBundle.getMsgInfoIndex())

        if (msgInfoIndex < stringToArrryLists.size) {
            msgDetail00.postValue(stringToArrryLists[msgInfoIndex])
           // msgCallback.msgCallback(stringToArrryLists[msgInfoIndex])

        } else {
            msgDetail00.postValue(stringToArrryLists[stringToArrryLists.size-1])
           // msgCallback.msgCallback(stringToArrryLists[stringToArrryLists.size-1])
        }

        PreferencesUtils.saveListToString(stringToArrryLists)
    }

    fun deleteAll() {
        if (stringToArrryList.size == 0) {
            toast(BusApplication.busContext, "无更多历史消息")
            return
        }
        stringToArrryList.clear()
        GMBInfoBundle.setMsgInfoIndex(0)
        msgDetail00.postValue(NoticeMsg("无更多历史消息" ,"","-99",true,false))
        PreferencesUtils.saveListToString(stringToArrryList)
       // msgCallback.msgCallback(NoticeMsg("无更多历史消息" ,"","-99",true))
    }
}